//
//  MessageSendCell.m
//  SouthChina
//
//  Created by Mr.Y on 14/12/9.
//  Copyright (c) 2014年 科创. All rights reserved.
//

#import "MessageSendCell.h"


@implementation MessageSendCell
{

    BOOL isShwoTimeLable;
    BOOL isShowSendFlag;
    BOOL isShowAudioFlag;
    BOOL isShowWaitFlag;


}

@synthesize  message;
@synthesize  msgSession;


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        _timeLabel =[[UILabel alloc]initWithFrame:CGRectZero];
        _headImage =[[UIImageView alloc]initWithFrame:CGRectZero];
        _audioFlag=[[UIImageView alloc]initWithFrame:CGRectZero];
        _bubbleImageView =[[UIImageView alloc]initWithFrame:CGRectZero];
        _imageContentView =[[UIImageView alloc]initWithFrame:CGRectZero];
        _sendFlag =[[UIImageView alloc]initWithFrame:CGRectZero];

        _textContentLable= [[M80AttributedLabel alloc]initWithFrame:CGRectZero];
        _textContentLable.backgroundColor=[UIColor clearColor];

        _progressView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectZero];


        
         _headImage.layer.masksToBounds = YES;
        _headImage.layer.cornerRadius = 8.0;
        
        _timeLabel.textColor = [UIColor lightGrayColor];
         _timeLabel.textAlignment = NSTextAlignmentCenter;
        [_timeLabel setBackgroundColor:[UIColor clearColor]];
        [_timeLabel setFont:[UIFont systemFontOfSize:TIME_CONTENT_FONT]];
        
        [self.contentView addSubview:_timeLabel];
        [self.contentView addSubview:_headImage];
        
        [self.contentView addSubview:_bubbleImageView];
        [_bubbleImageView addSubview:_audioFlag];
        [_bubbleImageView addSubview:_imageContentView];
        [_bubbleImageView addSubview:_textContentLable];
        
        [self.contentView addSubview:_sendFlag];
        [self.contentView addSubview:_progressView];


        [self setBackgroundColor:[UIColor clearColor]];
    }
    return self;
}

 

- (void)layoutSubviews{

    [super layoutSubviews];
    
    self.timeLabel.text =[self.message.upate_time stringFromDate];
    self.timeLabel.frame = CGRectMake(0, CELL_COM_GAP, SCREEN_WIDTH, TIME_LABEL_HEGIHT);
    self.headImage.frame = CGRectMake(SCREEN_WIDTH-HEAD_IMG_SIZE-CELL_COM_GAP, 2*CELL_COM_GAP+(isShwoTimeLable?TIME_LABEL_HEGIHT:0), HEAD_IMG_SIZE, HEAD_IMG_SIZE);
        
    int msgConentWidth = 0;
    int msgConentHeight = 0;
    
    if ([MEDIATYPE_PICTURE isEqualToString:message.mediaType]) {
        msgConentWidth = PICTURE_CELL_WIDTH+4;
        msgConentHeight = PICTURE_CELL_HEIGTH;
        self.imageContentView.frame = CGRectMake(8,8,PICTURE_CELL_WIDTH-16, PICTURE_CELL_HEIGTH-16);
        self.imageContentView.contentMode = UIViewContentModeScaleAspectFit;
        
        if ([message getValue:@"file"]) {
            NSURL *fileUrl = [NSURL fileURLWithPath:[message getValue:@"file"]];
            [self.imageContentView setImageWithURL:fileUrl placeholderImage:[UIImage imageNamed:@"empty_photo"]];

        }else{
            NSURL *urll = [NSURL URLWithString: [message.text hasPrefix:@"http"]?message.text:[NSString stringWithFormat:@"%@%@",DE.fileEngineeUrl,message.text]];
            [self.imageContentView setImageWithURL:urll placeholderImage:[UIImage imageNamed:@"empty_photo"]];
        }

    }
    else{
        NSString *textShow = @"";
        if ([MEDIATYPE_AUDIO isEqualToString:self.message.mediaType]) {
            textShow = [NSString stringWithFormat:@" %@秒",self.message.length];
        }else{
            textShow = self.message.text;
        }
        
        [BaseTableCell addCoreTextMessage:textShow label:self.textContentLable];
        
        CGSize contentSize = [self.textContentLable sizeThatFits:CGSizeMake(MAX_CONTENT_WIDTH, CGFLOAT_MAX)];
        msgConentWidth =contentSize.width + (isShowAudioFlag?2:1)*CELL_COM_GAP+TEXT_BUBBLE_GAP_RGIHT+(isShowAudioFlag?SOUNDFLAG_IMG_SIZE:0);
        msgConentHeight =contentSize.height + TEXT_BUBBLE_GAP*2 ;
        
        self.textContentLable.numberOfLines = 0;//表示label可以多行显示
        self.textContentLable.lineBreakMode = NSLineBreakByWordWrapping;//换行模式，与上面的计算保持一致。
        self.textContentLable.frame = CGRectMake((isShowAudioFlag?(2*CELL_COM_GAP+SOUNDFLAG_IMG_SIZE):TEXT_BUBBLE_GAP), TEXT_BUBBLE_GAP,contentSize.width, contentSize.height);

        self.audioFlag.frame =  CGRectMake(CELL_COM_GAP, TEXT_BUBBLE_GAP, SOUNDFLAG_IMG_SIZE, SOUNDFLAG_IMG_SIZE);
        [self.audioFlag setImage:[UIImage imageNamed:@"message_flag_sound_right"]];
        self.audioFlag.contentMode = UIViewContentModeScaleAspectFit;

    }
 
    self.bubbleImageView.frame = CGRectMake(SCREEN_WIDTH -3*CELL_COM_GAP-HEAD_IMG_SIZE-msgConentWidth,isShwoTimeLable?(TIME_LABEL_HEGIHT+2*CELL_COM_GAP):CELL_COM_GAP, msgConentWidth, msgConentHeight);
    [self.bubbleImageView setImage:[[UIImage imageNamed:@"message_right"]stretchableImageWithLeftCapWidth:18 topCapHeight:18]];
 
    self.sendFlag.frame =CGRectMake(SCREEN_WIDTH-3.5*CELL_COM_GAP-SENDFLAG_IMG_SIZE-msgConentWidth-HEAD_IMG_SIZE,isShwoTimeLable?(TIME_LABEL_HEGIHT+3*CELL_COM_GAP):2*CELL_COM_GAP,SENDFLAG_IMG_SIZE, SENDFLAG_IMG_SIZE);
    [self.sendFlag setImage:[UIImage imageNamed:@"send_fail"]];
    self.sendFlag.contentMode = UIViewContentModeScaleAspectFit;
    
    
   self.progressView.frame =CGRectMake(SCREEN_WIDTH-3.5*CELL_COM_GAP-SENDFLAG_IMG_SIZE-msgConentWidth-HEAD_IMG_SIZE,isShwoTimeLable?(TIME_LABEL_HEGIHT+3*CELL_COM_GAP):2*CELL_COM_GAP, 20,20);
    
    self.sendFlag.hidden = !isShowSendFlag;
    self.timeLabel.hidden = !isShwoTimeLable;
    self.audioFlag.hidden = !isShowAudioFlag;
    self.progressView.hidden=!isShowWaitFlag;
    
    if (isShowWaitFlag) {
        [self.progressView startAnimating];
    }else{
        [self.progressView stopAnimating];
    }
    
    
    NSString *headStr = [DE getGlobalVar:LOGIN_USER_HEADIMG];
    NSURL *url= [NSURL URLWithString: [headStr hasPrefix:@"http"]?headStr:[NSString stringWithFormat:@"%@%@",DE.fileEngineeUrl,headStr]];
    [self.headImage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
    
 
}


-(void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    UIView *view_bg = [[UIView alloc]initWithFrame:self.frame];
    view_bg.backgroundColor = [UIColor clearColor];
    self.selectedBackgroundView = view_bg;

}

-(void) setMsgCellValue:(Message*)msg preMessage:(Message*)preMessage session:(MessageSession*)session{

    self.message = msg;
    self.msgSession =session;
    if (preMessage) {
        NSTimeInterval secondsBetweenDates= [msg.upate_time timeIntervalSinceDate:preMessage.upate_time];
        isShwoTimeLable = secondsBetweenDates>=30.0f;
    }else{
        isShwoTimeLable =YES;
    }
    
    if ([@"false" isEqualToString:[msg getValue:@"isSucceed"]]) {
        isShowSendFlag = YES;
    }
    else{
        isShowSendFlag=NO;
    }
    
    if ([@"wait" isEqualToString:[msg getValue:@"isSucceed"]]) {
        isShowWaitFlag = YES;

    }else{
        isShowWaitFlag = NO;
        
    }

    if ([MEDIATYPE_AUDIO isEqualToString:msg.mediaType]) {
        isShowAudioFlag = YES;
    }
    else{
        isShowAudioFlag=NO;
    }

    isShowAudioFlag = [msg.mediaType isEqualToString:MEDIATYPE_AUDIO];
    
}


@end
